package cn.xiaolang.function.reflex;

import lombok.Data;

import java.lang.reflect.Field;

/**
 * 字段反射
 *
 * @author fuhw/Dean
 * @date 2019-12-23
 */
public class FieldDemo {

    public static void main(String[] args) {

        try {
            Class<?> clazz = Class.forName(MyField.class.getName());
            Field[] declaredFields = clazz.getDeclaredFields();

            MyField f = new MyField();
            f.setFieldName("F-NAME");
            f.setFieldValue("F-VALUE");

            Field field = declaredFields[1];
            field.setAccessible(true);
            field.set(f,"NEW-NAME");
            System.out.println(f);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    @Data
    static class MyField {

        private String fieldName;

        private String fieldValue;
    }
}
